package com.wzw.shoppingcart.resolver;

import com.wzw.shoppingcart.entity.Product;
import com.wzw.shoppingcart.resolver.input.ProductInput;
import com.wzw.shoppingcart.service.CartService;
import com.wzw.shoppingcart.service.ProductService;
import graphql.kickstart.tools.GraphQLMutationResolver;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;


/**
 *@author wzw
 * 2023/4/8
 */
@Component
@RequiredArgsConstructor
public class CartMutationResolver implements GraphQLMutationResolver {
    private final CartService cartService;
    private final ProductService productService;

    public boolean addProduct(ProductInput productInput) {
        Product product = productService.lambdaQuery()
                .eq(Product::getProductId,productInput.getProductId())
                .one();
        if (null != product && product.getStock() > 0 && product.getState() == 1) {
            return cartService.addProduct(product, productInput.getAmount(), productInput.getUserId());
        }
        return false;
    }

}
